package com.example.zhou.connect;

import com.example.zhou.application.MyApplication;
import com.example.zhou.callback.NewsInfoCallback;
import com.example.zhou.url.ZhihuUrl;
import com.example.zhou.zhihunews.MainActivity;

import java.io.IOException;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * Created by 11065 on 2016/11/1.
 */

public class NewsInfoConnect implements NewsInfoInterface {


    @Override
    public void getNewsInfo(String id, final NewsInfoCallback callback) {

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cache(MyApplication.cache).build();
        Request request = new Request.Builder().url(ZhihuUrl.NEWSINFO + id).build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onNewInfoFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callback.onNewInfoSuccess(response.body().string());
            }
        });

    }
}

interface NewsInfoInterface {
    void getNewsInfo(String id, NewsInfoCallback callback);
}
